﻿//-----------------------------------------------------------------------
// <copyright file="CorporateEmailAttribute.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.ContactManager.WebSite.Infrastructure.Validators
{
    using System.ComponentModel.DataAnnotations;

    public class CorporateEmailAttribute : ValidationAttribute
    {
        public string CorporateEmail { get; protected set; }

        public CorporateEmailAttribute(string corporateEmail)
        {
            this.CorporateEmail = corporateEmail;
        }

        public override bool IsValid(object value)
        {
            if(value == null)
                return true;
            var email = value as string;
            if (email == null)
                return true;
            return email.Contains(CorporateEmail);
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("The {0} provided you should be a corporate email from the domain ({1})", name, CorporateEmail);
        }
    }
}